package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Info;
import com.ruoyi.system.domain.InfoCategoryInfo;
import com.ruoyi.system.domain.InfoZhidingjieshushijian;
import com.ruoyi.system.mapper.InfoMapper;
import com.ruoyi.system.service.IInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 新闻Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-02
 */
@Service
public class InfoServiceImpl implements IInfoService 
{
    @Autowired
    private InfoMapper infoMapper;

    @Autowired
    private InfoCategoryInfoServiceImpl infoCategoryInfoService;

    @Autowired
    private InfoZhidingjieshushijianServiceImpl infoZhidingjieshushijianService;

    /**
     * 查询新闻
     * 
     * @param infoid 新闻主键
     * @return 新闻
     */
    @Override
    public Info selectInfoByInfoid(String infoid)
    {
        return infoMapper.selectInfoByInfoid(infoid);
    }

    public Map<String, Object>findOneInfo(String ppInfoId, String ppCategoryId) {
        return infoMapper.findOneInfo(ppInfoId,ppCategoryId);
    }

    public Map<String, Object>findCustomOne(String ppInfoId) {
        return infoMapper.findCustomOne(ppInfoId);
    }

    /**
     * 查询新闻列表
     * 
     * @param info 新闻
     * @return 新闻
     */
    @Override
    public List<Info> selectInfoList(Info info)
    {
        return infoMapper.selectInfoList(info);
    }

    /**
     * 新增新闻
     * 
     * @param info 新闻
     * @return 结果
     */
    @Override
    public int insertInfo(Info info)
    {
        return infoMapper.insertInfo(info);
    }

    /**
     * 修改新闻
     * 
     * @param info 新闻
     * @return 结果
     */
    @Override
    public int updateInfo(Info info)
    {
        return infoMapper.updateInfo(info);
    }

    /**
     * 批量删除新闻
     * 
     * @param infoids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteInfoByInfoids(String infoids)
    {
        return infoMapper.deleteInfoByInfoids(Convert.toStrArray(infoids));
    }

    /**
     * 删除新闻信息
     * 
     * @param infoid 新闻主键
     * @return 结果
     */
    @Override
    public int deleteInfoByInfoid(String infoid)
    {
        return infoMapper.deleteInfoByInfoid(infoid);
    }


    @Override
    public List<Map<String, Object>> findByCategoryIdPaging(String ppCategoryId, String ppInfotitle,String deptid ,int ppStartIndex, int ppPageSize) {
        return infoMapper.findByCategoryIdPaging(ppCategoryId,ppInfotitle, deptid,ppStartIndex, ppPageSize);
    }

    @Override
    public int countByCategoryIdPaging(String ppCategoryId,String ppInfotitle,String deptid) {
        Object mmCount = infoMapper.countByCategoryIdPaging(ppCategoryId,ppInfotitle,deptid);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    @Override
    public List<Map<String, Object>> findPassedByCategoryIdPaging(String ppCategoryId,String ppInfotitle, int ppStartIndex,
                                                                  int ppPageSize) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findPassedByCategoryIdPaging(enddate,ppCategoryId, ppInfotitle,ppStartIndex, ppPageSize);
    }

    public List<Map<String, Object>> findSYPassedByCategoryIdPaging(String ppCategoryId,int ppStartIndex,
                                                                  int ppPageSize) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findSYPassedByCategoryIdPaging(enddate,ppCategoryId,ppStartIndex, ppPageSize);
    }

    public List<Map<String, Object>> findPassedBySJCategoryId(String ppCategoryId) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findPassedBySJCategoryId(enddate,ppCategoryId);
    }

    @Override
    public List<Map<String, Object>> findPassedByCategoryIdAndTitlePaging(String ppInfotitle, int ppStartIndex,
                                                                  int ppPageSize) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findPassedByCategoryIdAndTitlePaging(enddate, ppInfotitle,ppStartIndex, ppPageSize);
    }

    @Override
    public int countPassedByCategoryIdAndTitlePaging(String ppInfotitle) {
        Object mmCount = infoMapper.countPassedByCategoryIdAndTitlePaging(ppInfotitle);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }


    @Override
    public List<Map<String, Object>> findByCategoryId(String ppCategoryId) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findByCategoryId(enddate,ppCategoryId);
    }

    @Override
    public int countPassedByCategoryIdPaging(String ppCategoryId,String ppInfotitle) {
        Object mmCount = infoMapper.countPassedByCategoryIdPaging(ppCategoryId,ppInfotitle);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    @Override
    public int countZhidingxinwen(String ppCategoryId) {
        Object mmCount = infoMapper.countZhidingxinwen(ppCategoryId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }


    @Override
    public List<Map<String, Object>> findPassedByCategoryIdAndDeptidPaging(String ppCategoryId,List<String> ppDeptid, int ppStartIndex,
                                                                  int ppPageSize) {
        int num = 5;   //增加天数
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fmt = format.format(date);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);// num为增加的天数，可以改变的
        date = calendar.getTime();
        String enddate = format.format(date);
        return infoMapper.findPassedByCategoryIdAndDeptidPaging(enddate,ppCategoryId, ppDeptid,ppStartIndex, ppPageSize);
    }

    @Override
    public int countPassedByCategoryIdAndDeptIdPaging(String ppCategoryId,List<String> ppDeptid) {
        Object mmCount = infoMapper.countPassedByCategoryIdAndDeptIdPaging(ppCategoryId,ppDeptid);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    @Override
    public int getMaxOrdernum(String ppCategoryId) {
        Object mmCount = infoMapper.getMaxOrdernum(ppCategoryId);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }


    @Override
    public List<Map<String, Object>> jiansuoInfo(String ppShijian, String ppFanwei,String ppNeirong,String ppshifouxuanzelanmu,List<String> ppCategoryidlist,String ppQi, String ppZhi,String ppPaixu,int ppStartIndex, int ppPageSize) {
        return infoMapper.jiansuoInfo(ppShijian,ppFanwei,ppNeirong,ppshifouxuanzelanmu,ppCategoryidlist,ppQi,ppZhi,ppPaixu,ppStartIndex,ppPageSize);
    }

    @Override
    public int countJiansuoInfo(String ppShijian, String ppFanwei,String ppNeirong,String ppshifouxuanzelanmu,List<String> ppCategoryidlist,String ppQi, String ppZhi) {
        Object mmCount = infoMapper.countJiansuoInfo(ppShijian,ppFanwei,ppNeirong,ppshifouxuanzelanmu,ppCategoryidlist,ppQi,ppZhi);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    @Override
    public List<Map<String, Object>> isZhiding(String ppInfoId){
        return infoMapper.isZhiding(ppInfoId);
    }

    @Override
    public boolean addCustom(String CategoryId,String Jianti,String Infotitle,String Infotitle2,String Summary,String Author,String Infocontemt,String Picurl,String Returnurl,Date Addtime,Date Releasetime,long Shifouzhiding,Date Zhidingshijian,
                         int Checkpass,int Ordernum)
    {
        try {

        Info info = new Info();
        String infoid = UUID.randomUUID().toString();
        info.setInfoid(infoid);
        info.setAddtime(Addtime);

        if ("".equals(Addtime)){
            info.setAddtime(new Date());
        }
        info.setInfotitle2(Infotitle2);
        info.setInfotitle(Infotitle);
        info.setSummary(Summary);
        info.setAuthor(Author);
        info.setInfocontent(Infocontemt);
        info.setJianti(Jianti);
        info.setPicurl(Picurl);
        info.setReturnurl(Returnurl);

        if ("".equals(Releasetime)){
            Releasetime=new Date("1111-11-11");
        }
        info.setReleasetime(Releasetime);

        info.setCheckpass(new Long(Checkpass));
        info.setClicknum("0");

        int addResult = insertInfo(info);
        if (addResult<1){
            throw new RuntimeException("新增新闻失败");
        }

        InfoCategoryInfo infoCategoryInfo = new InfoCategoryInfo();
        String infocategoryinfoid = UUID.randomUUID().toString();
        infoCategoryInfo.setId(infocategoryinfoid);
        infoCategoryInfo.setCategoryid(CategoryId);
        infoCategoryInfo.setInfoid(infoid);
        infoCategoryInfo.setShifouzhiding(Shifouzhiding);

        SysUser user = SecurityUtils.getLoginUser().getUser();
        String mmDeptId = user.getDeptId().toString();
        infoCategoryInfo.setText1(mmDeptId);
        /*long mmOrdernum = infoCategoryInfoService.getMaxOrdernum(CategoryId)+1;*/
        infoCategoryInfo.setOrdernum(new Long(Ordernum));

        int addInfoCategoryInfo = infoCategoryInfoService.insertInfoCategoryInfo(infoCategoryInfo);
        if (addInfoCategoryInfo<1){
            throw new RuntimeException("新增新闻栏目关联失败");
        }

        if (Shifouzhiding==1){
            InfoZhidingjieshushijian infoZhidingjieshushijian = new InfoZhidingjieshushijian();
            infoZhidingjieshushijian.setId(infocategoryinfoid);
            infoZhidingjieshushijian.setZdjssj(Zhidingshijian);

            int addInfoZhidingshijian =  infoZhidingjieshushijianService.insertInfoZhidingjieshushijian(infoZhidingjieshushijian);
            if (addInfoZhidingshijian<1){
                throw new RuntimeException("新增置顶时间失败");
            }

        }

        return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean modifyCustom(String InfoId,String OldCategoryId,String Jianti,String Infotitle,String Infotitle2,String Summary,String Author,String Infocontemt,String Picurl,String Returnurl,Date Addtime,Date Releasetime,
                             long Shifouzhiding,Date Zhidingjieshushijian,int Checkpass,int Ordernum)
    {
        try {
            Info info = selectInfoByInfoid(InfoId);
            info.setInfotitle2(Infotitle2);
            info.setInfotitle(Infotitle);
            info.setSummary(Summary);
            info.setAuthor(Author);
            info.setInfocontent(Infocontemt);
            info.setJianti(Jianti);
            info.setPicurl(Picurl);
            info.setReturnurl(Returnurl);
            if ("".equals(Releasetime)){
                Releasetime=new Date("1111-11-11");
            }
            info.setReleasetime(Releasetime);
            info.setCheckpass(new Long(Checkpass));

            if ("".equals(Addtime)){
                Addtime=new Date();
            }
            info.setAddtime(Addtime);


            int modifyResult = updateInfo(info);
            if (modifyResult<1){
                throw new RuntimeException("修改新闻失败");
            }

            InfoCategoryInfo infoCategoryInfo = new InfoCategoryInfo();
            infoCategoryInfo.setInfoid(InfoId);
            infoCategoryInfo.setCategoryid(OldCategoryId);
            InfoCategoryInfo infoCategoryInfo2 = infoCategoryInfoService.selectInfoCategoryInfoList(infoCategoryInfo).get(0);
            //infoCategoryInfo2.setCategoryid(NewCategoryId);
            infoCategoryInfo2.setShifouzhiding(Shifouzhiding);
            infoCategoryInfo2.setOrdernum(new Long(Ordernum));

            String infocategoryinfoid = infoCategoryInfo2.getId() == null ? "":infoCategoryInfo2.getId();

            int modifyInfoCategoryInfo = infoCategoryInfoService.updateInfoCategoryInfo(infoCategoryInfo2);
            if (modifyInfoCategoryInfo<1){
                throw new RuntimeException("修改新闻栏目关联失败");
            }

            if (Shifouzhiding ==1){
                InfoZhidingjieshushijian infoZhidingjieshushijian = infoZhidingjieshushijianService.selectInfoZhidingjieshushijianById(infocategoryinfoid);

                if (infoZhidingjieshushijian!=null){
                    infoZhidingjieshushijian.setZdjssj(Zhidingjieshushijian);
                    int modifyZhidingshijian = infoZhidingjieshushijianService.updateInfoZhidingjieshushijian(infoZhidingjieshushijian);

                    if (modifyZhidingshijian<1){
                        throw new RuntimeException("修改置顶结束时间失败");
                    }
                }else {
                    infoZhidingjieshushijian = new InfoZhidingjieshushijian();

                    infoZhidingjieshushijian.setId(infocategoryinfoid);
                    infoZhidingjieshushijian.setZdjssj(Zhidingjieshushijian);
                    int insertZhidingshijian = infoZhidingjieshushijianService.insertInfoZhidingjieshushijian(infoZhidingjieshushijian);

                    if (insertZhidingshijian<1){
                        throw new RuntimeException("设置置顶结束时间失败");
                    }
                }


            }

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteCustom(String InfoId,String CategoryId)
    {

        int deleteInfoCategoryInfo = infoCategoryInfoService.deleteByInfoidAndCategoryid(InfoId,CategoryId);
        if (deleteInfoCategoryInfo<1){
            throw new RuntimeException("删除新闻栏目关联失败");
        }

        return true;
    }
}
